package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.OrganizationPersonnelListItemBinding;
import com.shenyue.nbtownlet.databinding.WaitTaskListItemBinding;
import com.shenyue.nbtownlet.util.StringUtil;

public class OrganizationPersonnelListViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();
    public interface OrganizationPersonnelItemClickListener{
        void onItemClick(int position);
    }
    private OrganizationPersonnelItemClickListener listener;

    public OrganizationPersonnelListViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            OrganizationPersonnelListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.organization_personnel_list_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        OrganizationPersonnelListItemBinding root = viewHolder.getBinding();
        root.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onItemClick(position);
            }
        });
        JSONObject item = items.getJSONObject(position);
        //\u3000\u3000
        root.REALNAME.setText(item.getString("REALNAME"));
        root.ORGANNAMEDuty.setText(StringUtil.getValue(item.getString("ORGANNAME")) + "\u3000\u3000" + StringUtil.getValue(item.getString("Duty")));
        root.MOBILE.setText(item.getString("MOBILE"));
        String TEL = StringUtil.getValue(item.getString("TEL"));
        if(!TEL.contains("0574-"))
            TEL = "0574-" + TEL;
        root.TEL.setText(TEL);

        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OrganizationPersonnelItemClickListener listener){
        this.listener = listener;
    }

    private class ViewHolder {
        private OrganizationPersonnelListItemBinding binding;

        public ViewHolder(OrganizationPersonnelListItemBinding binding) {
            this.binding = binding;
        }

        public OrganizationPersonnelListItemBinding getBinding() {
            return binding;
        }
    }
}
